package com.note.service;

import com.note.mapper.NoteMapper;
import com.note.pojo.Note;
import com.note.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
public class NoteServiceImpl implements NoteService {
    @Autowired
    private NoteMapper noteMapper;

    @Override
    public Integer insertNote(Note note, HttpSession session) {
        note.setCreatetime(new Date());
        User user = (User) session.getAttribute("user");
        note.setUserid(user.getId());  //当前用户id
        return noteMapper.insertNote(note);
    }

    @Override
    public List<Note> listNoteByUid(Integer userid) {
        List<Note> notes = noteMapper.getNotesByUid(userid);
        return notes;
    }

    @Override
    public Note getNoteById(Integer id) {
        return noteMapper.getNoteById(id);
    }

    @Override
    public List<Note> listByUidTy(Integer userid, int type) {
        return noteMapper.getNotesByUidTy(userid,type);
    }

    @Override
    public void deleteNoteById(Integer id) {
        noteMapper.deleteNoteById(id);
    }

    @Override
    public void alterType(Integer id, int type) {
        noteMapper.alterType(id,type);
    }

    @Override
    public void alterNote(Note note) {
        note.setCreatetime(new Date());
        System.out.println(note);
        noteMapper.updateNote(note);
    }

    @Override
    public List<Note> getNotesByType(int type) {
        return noteMapper.getNotesByType(type);
    }
}
